package com.photoeditor.demo.ui.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.photoeditor.demo.util.ImageRectUtils;

/**
 * 实现滤镜效果图动画的ImageView
 */
public class RenderingView extends ImageView {

    public static final int DEFUALT_ANIMATION_DURATION = 1300;

    private Context mContext;

    public Bitmap getTopImage() {
        return mTopImage;
    }

    private Bitmap mTopImage;
    private float mProgress = 0f;
    private Rect mSrc = new Rect();
    private RectF mDst = new RectF();
    private RectF mImageRect = new RectF();
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    private ObjectAnimator mAnimatior = ObjectAnimator.ofFloat(this, "progress", 1);

    private long downTime;

    public RenderingView(Context context) {
        super(context);
        init(context);
    }

    public RenderingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RenderingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
//        if (changed && getDrawable() != null) {
        RectF rect = ImageRectUtils.getViewRect(this);
        mImageRect = ImageRectUtils.getDrawableRect(this);
        mImageRect.offset(-rect.left, -rect.top);
//        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mTopImage != null) {
            mSrc.left = (int) (mTopImage.getWidth() * mProgress + 0.5f);
            mSrc.top = 0;
            mSrc.right = mTopImage.getWidth();
            mSrc.bottom = mTopImage.getHeight();
            mDst.set(mImageRect);
            mDst.left = mDst.left + mDst.width() * mProgress;
            canvas.drawBitmap(mTopImage, mSrc, mDst, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mTopImage != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                /*mAnimatior.cancel();
                mProgress = 0;
                invalidate();*/
                    downTime = event.getDownTime();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    if (event.getEventTime() - downTime > 500) {
                        mAnimatior.cancel();
                        mProgress = 0;
                        invalidate();
                        return true;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    mProgress = 0;
                    startAnimator();
                    return true;

            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 设置效果预览图
     *
     * @param bottomImage 底下的原图
     * @param topImage    顶上的效果图
     */
    public void setRenderingImages(Bitmap bottomImage, Bitmap topImage) {
        mTopImage = topImage;
        mProgress = 1f;
        setImageBitmap(bottomImage);
    }

    public void setBottomImage(Bitmap bottomImage) {
        setImageBitmap(bottomImage);
        // 隐藏默认图，解决左侧有一像素白边bug
        View child = ((ViewGroup) getParent()).getChildAt(0);
        if (child != this) {
            child.setVisibility(GONE);
        }
    }

    public void setTopImage(Bitmap topImage) {
        mTopImage = topImage;
        mProgress = 1f;
        invalidate();
    }

    /**
     * 开始动画，默认长度为800毫秒
     */
    public void startAnimator() {
        startAnimator(DEFUALT_ANIMATION_DURATION);
    }

    /**
     * 开始动画
     *
     * @param duration
     */
    public void startAnimator(long duration) {
        mProgress = 0;
        mAnimatior.setDuration(duration);
        mAnimatior.start();
    }

    public float getProgress() {
        return mProgress;
    }

    public void setProgress(float progress) {
        mProgress = progress;
        invalidate();
    }
}
